//
//  HJAppointmentMyGuestsController.m
//  Keping
//
//  Created by apple on 2017/11/15.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJAppointmentMyGuestsController.h"
#import "HJAppointmentsCell.h"
#import "HJAppointmentsDetailController.h"
#import "HJAppointmentRequest.h"
#import "HJAppointmentModel.h"
#import <MJRefresh/MJRefresh.h>

@interface HJAppointmentMyGuestsController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak)UITableView*tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation HJAppointmentMyGuestsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
-(void)setUI
{
    CGRect rect = self.view.bounds;
    rect.size.height = rect.size.height - 45;
    UITableView*tableView=[[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor colorWithHexString:@"#f2f1f6"];
    tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self getData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex++;
        [self getData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (void)getData {
    [super getData];
    [self showLoading];
    HJAppointmentRequest *request = [[HJAppointmentRequest alloc] init];
    request.params = @{@"pageSize":@(20),@"page":@(self.pageIndex)};
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            if (weakSelf.pageIndex == 1) {
                [weakSelf.dataArr removeAllObjects];
            }
            NSArray *array = [dict objectForKey:@"inviteEntityItemViews"];
            for (NSDictionary *tmp in array) {
                HJAppointmentModel *model = [HJAppointmentModel modelWithDictionary:tmp];
                [weakSelf.dataArr addObject:model];
            }
            [weakSelf.tableView reloadData];
        } else {
            [weakSelf showText:[dict objectForKey:@"msg"]];
            [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:status];
        }
        
        if (weakSelf.pageIndex == 1) {
            [weakSelf.tableView.mj_header endRefreshing];
        } else {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        
        if (weakSelf.pageIndex == 1) {
            [weakSelf.tableView.mj_header endRefreshing];
        } else {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        [weakSelf showError:request.error];
    }];
}

#pragma mark --UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJAppointmentsCell*cell=[HJAppointmentsCell cellWithTableView:tableView];
    cell.appointment = self.dataArr[indexPath.row];
    WEAKSELF
    cell.hjAppointmentStatus = ^(HJAppointmentModel *appointment, BOOL isaccept) {
        [weakSelf showLoading];
        HJAppoInviteGuestRequest *request = [[HJAppoInviteGuestRequest alloc] initWithGuestID:appointment.id];
        request.params = @{@"status":isaccept ? @"ing" : @"reject"};
        [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
            [weakSelf hideHUD];
            NSDictionary *dict = request.responseObject;
            NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
            if (status == 200) {
                appointment.status = isaccept ? @"ing" : @"reject";
                [weakSelf.dataArr replaceObjectAtIndex:indexPath.row withObject:appointment];
                [weakSelf.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
                [weakSelf showSuccess:[dict objectForKey:@"msg"]];
            } else {
                [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:status];
            }
        } failure:^(__kindof KBaseRequest * _Nonnull request) {
            [weakSelf hideHUD];
            [weakSelf showError:request.error];
        }];
    };
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HJAppointmentsDetailController*vc=[HJAppointmentsDetailController new];
    vc.indexPath = indexPath;
    WEAKSELF
    vc.uploadDataSource = ^(NSIndexPath *indexPath) {
//            [weakSelf.dataArr replaceObjectAtIndex:indexPath.row withObject:obj];
//            [weakSelf.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
        [weakSelf getData];
    };
    vc.AppointmentModel = self.dataArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return K_FactorH(90);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return K_FactorH(10);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

@end
